package net.wanho.shop.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.shop.entity.Goods;
import net.wanho.shop.service.GoodsService;
import net.wanho.shop.vo.Page;
import net.wanho.shop.vo.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-09-24 9:13
 * Description：<描述>
 */
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController {

    private final GoodsService goodsService;

    @GetMapping("/page")
    public R findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                      Goods goods) {
        Page<Goods> page = goodsService.findPage(pageNum, pageSize, goods);
        return R.success(page);
    }

    @PostMapping
    public R save(@RequestBody Goods goods) {
        goodsService.save(goods);
        return R.success("保存商品成功");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Integer id) {
        Goods goods = goodsService.findById(id);
        return R.success(goods);
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        goodsService.remove(id);
        return R.success("删除商品成功");
    }

    @DeleteMapping("/del/batch")
    public R removeBatch(@RequestBody List<Integer> ids) {
        goodsService.removeBatch(ids);
        return R.success("批量删除商品成功");
    }

    @PutMapping("/status/{id}/{state}")
    public R updateStatus(@PathVariable Integer id, @PathVariable Integer state) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setState(state);
        goodsService.save(goods);
        return R.success("修改商品状态成功");
    }
}
